/* eslint-disable no-undef */
import tinymce from 'tinymce/tinymce'
tinymce.PluginManager.add('fontName', function (e) {
  var n = '字体',
    r = tinymce.util.Tools.resolve('tinymce.util.Tools'),
    i = []
  e.on('init', function () {
    e.formatter.register({
      fontName: {
        selector: 'span,p,h1,h2,h3,h4,h5,h6,td,th,ul,ol,li,table,div',
        styles: {
          'font-family': '%value'
        }
      }
    })
  })
  const o = function (t) {
    e.undoManager.transact(function () {
      if ((e.focus(), t))
        e.formatter.apply('fontName', {
          value: t
        })
      else {
        var n = e.selection.getSelectedBlocks()
        r.each(n, (e) => {
          ;(e.style.fontFamily = null), e.removeAttribute('data-mce-style')
        })
      }
    })
  }
  var a = function (t) {
      var n = e.selection.getSelectedBlocks(),
        i = 0
      return (
        r.each(n, (t) => {
          0 === i && (i = e.dom.getStyle(t, 'font-family') ? e.dom.getStyle(t, 'font-family') : 0)
        }),
        i === t
      )
    },
    s = function (t, n) {
      var r = e.selection.getNode(),
        i = r ? window.getComputedStyle(r).fontFamily : '宋体'
      return i.toLowerCase() === n.toLowerCase()
    },
    l = function () {
      const font_formats = "宋体=宋体;黑体=黑体;Times New Roman=Times New Roman";
      return (
        i.length ||
          (font_formats.split(';').forEach((e) => {
            let t = e.split('=')
            i.push({
              type: 'togglemenuitem',
              text: t[0],
              value: t[1],
              active: a(t[1]),
              onAction: () => {
                o(t[1])
              },
              onSetup: (e) => {
                e.setActive(s(e, t[1]))
              }
            })
          })
          // i.unshift({
          //   type: 'togglemenuitem',
          //   text: '未设置',
          //   value: '',
          //   active: a(''),
          //   onAction: () => {
          //     o('')
          //   },
          //   onSetup: (e) => {
          //     e.setActive(s(e, ''))
          //   }
          // })
          ),
        i
      )
    },
    u = function () {
      if (e && e.selection) {
        var t = Array.from(e.selection.getSelectedBlocks())
        if (t[0].style.fontFamily) return t[0].style.fontFamily || ''
      }
      return '黑体' // 因为禁用临时显示黑体
      // return '字体'
    },
    c = (e) => {
      let t = !1,
        n = e.selection.getNode(),
        r = e.dom.getParent(n, '.info-block')
      // if (((t = !r || e.dom.hasClass(r, 'fixed') || void 0 !== r.dataset.enlock || !r.isContentEditable), t)) {
      //   let r = e.dom.getParent(n, '.ol-list') || e.dom.getParent(n, '.appendix-list')
      //   r && !e.dom.hasClass(r, 'disabled') && (t = !1)
      // }
      return t
    }
  return (
    e.ui.registry.addMenuButton('fontName', {
      text: u(),
      tooltip: n,
      fetch: (e) => {
        e(l())
      },
      onSetup: (t) => {
        var n = () => {
          t.setDisabled(true) // M1阶段暂时禁用
        }
        return e.on('NodeChange', n), () => e.off('NodeChange', n)
      }
    }),
    {
      getMetadata: function () {
        return {
          name: n,
          url: 'http://sfrj.cn'
        }
      }
    }
  )
})
